package shell.gateway;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 解析《WAP网关IP地址汇总》
 * @author wangxinning
 *
 */
public class ParseGatewapExcel {

	public static void main( String[] args ) throws FileNotFoundException, IOException
	{
		//1.定位文件
		String excelFileString = "F:\\WAP网关IP地址汇总V7-20121127-数-JSJ12153.xls";
		File excelFile = new File( excelFileString );
		
		//2.excel解析
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excelFile));
		HSSFSheet sheet = workbook.getSheet("全网WAP网关IP地址汇总");
		
		System.out.println( sheet );
		
		for( int i = 0; i < sheet.getPhysicalNumberOfRows(); i++)
		{
			HSSFCell cmwapCell = sheet.getRow(i).getCell(8);
			HSSFCell cmnetCell = sheet.getRow(i).getCell(9);
			
			if( cmwapCell != null && StringUtils.isNotBlank(cmwapCell.getStringCellValue()) )
			{
				System.out.println( StringUtils.trimToEmpty(cmwapCell.getStringCellValue()) );
			}
			
			if( cmnetCell != null && StringUtils.isNotBlank(cmnetCell.getStringCellValue()) )
			{
				System.out.println( "****" + StringUtils.trimToEmpty(cmnetCell.getStringCellValue()) );
			}
			
			
			String s = "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))";
			System.out.println( "AAAAA" + matcher(cmnetCell.getStringCellValue(), s, 0) );
		}
		
		
	}	
	
	
	
	
	public static String matcher( String str, String regex, int groupid )
	{
		Pattern p = Pattern.compile(regex);   
        Matcher m = p.matcher(str);
		
        while(m.find())
        {  
        	if( m.groupCount() < groupid )
        	{
        		return null;
        	}
        	
            return m.group(groupid);
        }
        
        return null;
	}
}